package org.testis.testdriver.operations;

import org.testis.testdriver.*;
import org.testis.data.TestStepDataObject;
import org.testis.services.TestStepExecutor;
import org.testis.testdriver.AtofEnumaration.DebuggingMode;
import java.util.HashMap;
import java.util.LinkedHashMap;

import org.testis.utils.middleware.GlobalDataHandler;
import org.testis.utils.middleware.LanguageDataHandler;


/**
 *
 * @author barisergun
 */
public class TestPartOperationSetTestPlanGlobals extends TestPartOperationDecorator
{

    private TestPart partInUse;


    public TestPartOperationSetTestPlanGlobals(TestPart testPart)
    {
        partInUse = testPart;
    }


    @Override
    public boolean runOperation(DebuggingMode mode) throws InterruptedException
    {
        if (super.runOperation(mode)) {
            return runSpecificBehaviour();
        }
        else {
            return false;
        }
    }


    /**
     * This method is used to drive the specific Tasks that belong to this decorator
     * operation.
     * 
     * @return boolean the result of operation. This result will affect the operation
     * of linked decorator tasks. 
     */
    private boolean runSpecificBehaviour()
    {

        TestPlanGlobalsHashMap.createInstance().resetMaps();
        LanguageDataHandler langHandler = new LanguageDataHandler();
        GlobalDataHandler globalHandler = new GlobalDataHandler();
        Integer langId = ((StepContainer) partInUse).getLanguageId();

        if (langId != null) {
            HashMap<String, String> langMap = langHandler.getLanguagePairsAsMap(
                langId);

            if (langMap != null) {
                TestPlanGlobalsHashMap.createInstance().putAllLangValues(langMap);
            }

        }
        Integer profileId = ((StepContainer) partInUse).getProfileId();

        if (profileId != null) {
            HashMap<String, String> globalMap = globalHandler.
                getProfileDetailsAsMap(
                profileId);


            if (globalMap != null) {
                TestPlanGlobalsHashMap.createInstance().putAllGlobalsValues(
                    globalMap);
            }
        }

        if (((StepContainer) partInUse).getSerialConnData() != null) {
            if (!((StepContainer) partInUse).getSerialConnData().isEmpty()) {

                TestStepDataObject myData = new TestStepDataObject();
                 myData.setStoreResultPostfix(partInUse.getStoreResultPostfix());
                myData.setMessageId(partInUse.getPartGuid());
                myData.setTestStepId(partInUse.getPartId());
                myData.setModuleType(AtofEnumaration.AtofModule.SerialPortModule.
                                     name());


                myData.setActionType(AtofEnumaration.AtofSerialPortModuleAction.StartConnection.
                                     name());
                myData.setMacAddress(((StepContainer) partInUse).
                                     getTestPcMacAddr());
                myData.setAsynchCall(false);
                myData.setTestStepDataName("Listen Serial Connection");
                myData.setTestStepDataId(partInUse.getPartGuid());
                LinkedHashMap<String, String> cloneOfMap =
                    new LinkedHashMap<String, String>();
                cloneOfMap.putAll(
                    ((StepContainer) partInUse).getSerialConnData());
                myData.setDataToApply(cloneOfMap);
                TestStepExecutor executor = new TestStepExecutor();
                executor.send(myData,
                              true);

            }
        }


        return true;
    }
}
